﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;

namespace AutoIndustryFiles
{
    class chintScraper : FileLinksScraper2
    {
        public chintScraper()
        {
            this.MainPage = "http://www.chint.net/product/product.php?l=cn";
            this.CompanyName = "正泰电器";
            this.CompanyUrl = "http://www.chint.net/product/product.php?l=cn";
            this.ColumnName = "首页 > 产品 & 解决方案";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);            
        }


        public override void GenerateAllLinks(string param)
        {
            /***
             * //li[@class="parent open active"]/[a1] 第一级
             * //li[@class="parent open active"]//li[@class="active"] 第二级
             * //div[@class="pLi"]//li/div/a 产品级 @href  @title
             * //div[@id="tab2"]//p/a @href innerText 样本
             * //div[@id="tab3"]//p/a @href innerText 证书
            ***/

            var files = from file in Directory.EnumerateFiles(@"D:\外包项目\自动化资料下载\Output\chint\", "*.php", SearchOption.AllDirectories)
                        orderby file ascending
                        select new
                        {
                            File = file
                        }
;
            if (files.Count() == 0)
            {
                return;
            }

            HtmlDoc doc = new HtmlDoc();
            foreach (var f in files)
            {
                string html = File.ReadAllText(f.File, Encoding.GetEncoding("UTF-8"));

                doc.LoadHtml(html);

                HtmlAgilityPack.HtmlNodeCollection hnc_product = doc.DocumentNode.SelectNodes(@"//div[@class=""pLi""]//li/div/a");

                if (hnc_product == null)
                    continue;

                string leve0 = doc.DocumentNode.SelectSingleNode(@"//li[@class=""parent open active""]/a[1]").InnerText;
                string leve1 = doc.DocumentNode.SelectSingleNode(@"//li[@class=""parent open active""]//li[@class=""active""]/a").InnerText;

                foreach (HtmlAgilityPack.HtmlNode node in hnc_product)
                {
                    string productName = node.GetAttributeValue("title", "");
                    string productUrl = node.GetAttributeValue("href", "");
                    productUrl = FormatUrl(productUrl, "http://www.chint.net/product/");

                    bool bv = false;
                    UpdateMsg("chintScraper", productUrl, ref bv);
                    string html_product = WebFuncs.OpenUrlEx(productUrl, "UTF-8");
                    HtmlDoc docProduct = new HtmlDoc();
                    docProduct.LoadHtml(html_product);
                    HtmlAgilityPack.HtmlNodeCollection hnc_docs = docProduct.DocumentNode.SelectNodes(@"//div[@id=""tab2""]//p/a");
                    if (hnc_docs != null)
                        foreach (HtmlAgilityPack.HtmlNode node_doc in hnc_docs)
                        {
                            FileLinks.Add(new FileLink(
                                FileLinks.Count().ToString(),
                                FormatUrl(node_doc.GetAttributeValue("href", ""), "http://www.chint.net"),
                                node_doc.InnerText,
                                leve0 + "->" + leve1 + "->" + productName,
                                true,
                                "样本"));
                        }

                    hnc_docs = docProduct.DocumentNode.SelectNodes(@"//div[@id=""tab3""]//p/a");
                    if (hnc_docs != null)
                        foreach (HtmlAgilityPack.HtmlNode node_doc in hnc_docs)
                        {
                            FileLinks.Add(new FileLink(
                                FileLinks.Count().ToString(),
                                FormatUrl(node_doc.GetAttributeValue("href", ""), "http://www.chint.net"),
                                node_doc.InnerText,
                                leve0 + "->" + leve1 + "->" + productName,
                                true,
                                "认证"));
                        }
                }
            }
        }
    }
}
